import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * @ClassName CollectionsDemo02
 * @Description Comparator比较器
 * @Author Li
 * @Date 2022/8/5 9:16
 * @ModifyDate 2022/8/5 9:16
 * @Version 1.0
 */
public class CollectionsDemo02 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        // 往集合中添加一些元素
        Collections.addAll(list, "a", "b", "c", "d", "e", "f", "g");

        // 【前面的减后面的】代表升序，【后面的减前面】的代表降序
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.charAt(0) - o2.charAt(0);
            }
        });
        System.out.println("升序排序：" + list);

        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.charAt(0) - o1.charAt(0);
            }
        });
        System.out.println("降序排序：" + list);
    }
}
